﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using Tenancy.wxpay.lib;

namespace Tenancy.wxpay.business
{ /// <summary>
  /// 支付结果通知回调处理类
  /// 负责接收微信支付后台发送的支付结果并对订单有效性进行验证，将验证结果反馈给微信支付后台
  /// </summary>
    public class ResultNotify : Notify
    {
        //private string appKey = RongCloudConfig.appKey;
        //private string appSecret = RongCloudConfig.appSecret;

        public ResultNotify(Page page)
            : base(page)
        {
        }

        string UID = string.Empty;
        string Transactionid = string.Empty;
        string ResultCode = string.Empty;
        string ReturnCode = string.Empty;
        string Openid = string.Empty;
        string TradeType = string.Empty;
        string BankType = string.Empty;
        int TotalFee = 0;
        int CashFee = 0;
        string OutTradeNo = string.Empty;
        string TimeEnd = string.Empty;
        DateTime Cjsj = DateTime.Now;
        string ErrCode = string.Empty;
        string ErrCodeDes = string.Empty;

        public override void ProcessNotify()
        {
            WxPayData notifyData = GetNotifyData();

            //检查支付结果中transaction_id是否存在
            if (!notifyData.IsSet("transaction_id"))
            {
                //若transaction_id不存在，则立即返回结果给微信支付后台
                WxPayData res = new WxPayData();
                res.SetValue("return_code", "FAIL");
                res.SetValue("return_msg", "支付结果中微信订单号不存在");
                Log.Error(this.GetType().ToString(), "The Pay result is error : " + res.ToXml());
                page.Response.Write(res.ToXml());
                page.Response.End();
            }

            string transaction_id = notifyData.GetValue("transaction_id").ToString();

            //查询订单，判断订单真实性
            if (!QueryOrder(transaction_id))
            {
                //若订单查询失败，则立即返回结果给微信支付后台
                WxPayData res = new WxPayData();
                res.SetValue("return_code", "FAIL");
                res.SetValue("return_msg", "订单查询失败");
                Log.Error(this.GetType().ToString(), "Order query failure : " + res.ToXml());
                page.Response.Write(res.ToXml());
                page.Response.End();
            }
            //查询订单成功
            else
            {
                WxPayData res = new WxPayData();
                res.SetValue("return_code", "SUCCESS");
                res.SetValue("return_msg", "OK");
                // System.IO.File.WriteAllText("F:\\1-web\\157-cxgznew.hbung.com\\weixin.txt", "微信支付成功");
                Log.Info(this.GetType().ToString(), "order query success : " + res.ToXml());
                page.Response.Write(res.ToXml());
                page.Response.End();
            }
        }

        //查询订单
        private bool QueryOrder(string transaction_id)
        {
            string sErr = "";
            WxPayData req = new WxPayData();
            req.SetValue("transaction_id", transaction_id);
            WxPayData res = WxPayApi.OrderQuery(req);
            if (res.GetValue("return_code").ToString() == "SUCCESS" && res.GetValue("result_code").ToString() == "SUCCESS")
            {
                UID = Guid.NewGuid().ToString();
                Transactionid = transaction_id;
                ResultCode = res.GetValue("result_code").ToString();
                ReturnCode = res.GetValue("return_code").ToString();
                Openid = res.GetValue("openid").ToString();
                TradeType = res.GetValue("trade_type").ToString();
                BankType = res.GetValue("bank_type").ToString();
                TotalFee = Convert.ToInt32(res.GetValue("total_fee").ToString());
                CashFee = Convert.ToInt32(res.GetValue("cash_fee").ToString());
                OutTradeNo = res.GetValue("out_trade_no").ToString();
                TimeEnd = res.GetValue("time_end").ToString();
                Cjsj = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
               // bool istrue = wxjsmessM.AddHbWXJieSMessage(hbwxjsmessE, out sErr);
                  //  System.IO.File.WriteAllText("E:\\1-Web\\1-cxgz.hbung.com\\weixinNew.txt", "count:"+ count+","+ "shouruTW:"+ shouruTW+","+ "shouruHD:"+ shouruHD+","+ "shourucount:"+ shourucount+","+ "uptamountcountTW:"+ uptamountcountTW+","+ "uptamountcountHD:"+ uptamountcountHD+","+ "MoneyAvg:"+ MoneyAvg+","+ "TiWenUserID:"+ TiWenUserID2+","+"telphone:"+ new HbMember(TiWenUserID2).Telphone+","+"out_trand_id:"+ res.GetValue("out_trade_no").ToString());
                    //  System.IO.File.WriteAllText("E:\\1-Web\\1-cxgz.hbung.com\\weixinNew.txt", "微信支付回调:"+"count:"+count+","+ "shourucount:"+ shourucount+","+ "uptamountcount:"+ uptamountcount+","+"istrue:"+istrue);
                
                //System.IO.File.WriteAllText("F:\\1-web\\157-cxgznew.hbung.com\\weixin.txt", "微信支付成功:"+count);
               
                return true;
            }
            else
            {
                UID = Guid.NewGuid().ToString();
               Transactionid = transaction_id;
                ResultCode = res.GetValue("result_code").ToString();
                ReturnCode = res.GetValue("return_code").ToString();
                ErrCode = res.GetValue("err_code").ToString();
                ErrCodeDes = res.GetValue("err_code_des").ToString();
                Openid = res.GetValue("openid").ToString();
                TradeType = res.GetValue("trade_type").ToString();
                BankType = res.GetValue("bank_type").ToString();
                TotalFee = Convert.ToInt32(res.GetValue("total_fee").ToString());
                CashFee = Convert.ToInt32(res.GetValue("cash_fee").ToString());
                OutTradeNo = res.GetValue("out_trade_no").ToString();
                TimeEnd = res.GetValue("time_end").ToString();
                Cjsj = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
              //  bool istrue = wxjsmessM.AddHbWXJieSMessage(hbwxjsmessE, out sErr);
                return false;
            }
        }
    }
}